package com.zsrd.self.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsrd.self.base.Result;
import com.zsrd.self.entity.SelfUser;
import com.zsrd.self.service.SelfUserService;
import com.zsrd.self.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <pre>
 *  小程序保存及获取用户信息
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/2/29 8:45
 */
@RestController
@RequestMapping("/user")
public class SelfUserController {

    @Autowired
    private SelfUserService selfUserService;

    @Autowired
    private WxPayService wxPayService;


    @PostMapping("/list")
    public Result list(){
        List<SelfUser> list = selfUserService.list();
        return Result.success(list);
    }

    //记录小程序用户
    @PostMapping("/save")
    public Result save(@RequestBody SelfUser selfUser){
        selfUserService.save(selfUser);
        return Result.success();
    }

    @PostMapping("/getByWxId")
    public Result getByWxId(@RequestBody SelfUser selfUser){
        //前端传入openid，通过接口获取unionid
        if(StringUtils.isEmpty(selfUser.getUnionId())){
            return Result.error("传入UnionId不能为空");
        }
        List<SelfUser> selfUsers = selfUserService.list(new QueryWrapper<SelfUser>().eq("union_id", selfUser.getUnionId()));
        if (CollectionUtils.isEmpty(selfUsers)){
            return Result.error("未找到用户填写信息");
        }else{
            return Result.success(selfUsers.get(0));
        }
    }
}
